package cn.jingdalei.system.controller;

import cn.jingdalei.basic.util.AjaxResult;
import cn.jingdalei.basic.util.PageList;
import cn.jingdalei.system.domain.SystemDictionaryType;
import cn.jingdalei.system.query.SystemDictionaryTypeQuery;
import cn.jingdalei.system.service.ISystemDictionaryTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

// @Controller
@RestController  // (controller + responseBoby[返回json格式] )
@RequestMapping("/dictionarytype")
@Api(tags = "数据字典类型接口",description = "店铺接口详细描述")
public class SystemDictionaryTypeController {

    @Autowired
    
    private ISystemDictionaryTypeService systemDictionaryTypeService;

    /**
     * 添加或者修改
     * @param systemDictionaryType
     * @return
     */
    @PutMapping
    @ApiOperation(value = "店铺添加或修改",notes = "如果有id就是修改否则就是添加")
    public AjaxResult addOrUpdate(@RequestBody  SystemDictionaryType systemDictionaryType){
        try {
            if (systemDictionaryType.getId()==null){
                systemDictionaryTypeService.add(systemDictionaryType);
            }else {
                systemDictionaryTypeService.update(systemDictionaryType);
            }
            return AjaxResult.me();
        } catch (Exception e){
            e.printStackTrace();
            // 避免多次创建对象
            return  AjaxResult.me().setMessage("操作失败.");
        }
    }
    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除",notes = "如果有id就是修改否则就是添加")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            systemDictionaryTypeService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败。");
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            systemDictionaryTypeService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            // 链式编程思想
            return AjaxResult.me().setMessage("删除失败.");
        }
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "根据id查询",notes = "如果有id就是修改否则就是添加")
    public SystemDictionaryType findById(@PathVariable("id") Long id){
        return systemDictionaryTypeService.queryById(id);
    }

    /**
     *  查询所有
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询全部店铺信息",notes = "如果有id就是修改否则就是添加")
    public List<SystemDictionaryType> findAll(){
        return systemDictionaryTypeService.queryAll();
    }

    /**
     * 分页查询
     * axios get不支持传对象
     * @param query
     * @return
     */
    @PostMapping         // @GetMapping()不能传对象
    @ApiOperation(value = "分页查询店铺信息",notes = "如果有id就是修改否则就是添加")
    public PageList<SystemDictionaryType> findPage(@RequestBody SystemDictionaryTypeQuery query){
        return systemDictionaryTypeService.queryData(query);
    }

}
